﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace Jayden.Windows.PokerWatcher
{
	public partial class PokerTableOverlay : Form
	{
		protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
		[DllImport("user32.dll", CharSet = CharSet.Unicode)]
		protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
		[DllImport("user32.dll", CharSet = CharSet.Unicode)]
		protected static extern int GetWindowTextLength(IntPtr hWnd);
		[DllImport("user32.dll")]
		protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
		[DllImport("user32.dll")]
		protected static extern bool IsWindowVisible(IntPtr hWnd);
		[DllImport("user32.dll")]
		public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);

		public struct Rect
		{
			public int Left;
			public int Top;
			public int Right;
			public int Bottom;
		}

		public class TableLayoutInformations
		{
			public Size Size = new Size(649, 469);
			public Point[] Seats = new Point[]
			{
				new Point(191, 46),
				new Point(401, 46),
				new Point(83, 106),
				new Point(514, 106),
				new Point(543, 201),
				new Point(54, 201),
				new Point(458, 295),
				new Point(139, 295),
				new Point(296, 309)
			};
			public Size SeatSize = new Size(57, 57);

			public double GetRatio(int width, int height)
			{
				return width * 1.0 / Size.Width;
			}
			public Size GetSeatSize(double ratio)
			{
				return new Size((int)(SeatSize.Width * ratio), (int)(SeatSize.Height * ratio));
			}
			public Point GetSeat(int index, double ratio)
			{
				return new Point((int)(Seats[index].X * ratio), (int)(Seats[index].Y * ratio));
			}
		}

		protected bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
		{
			int size = GetWindowTextLength(hWnd);
			if (size++ > 0 && IsWindowVisible(hWnd))
			{
				StringBuilder sb = new StringBuilder(size);
				GetWindowText(hWnd, sb, size);
				string windowsCaption = sb.ToString();
				if (windowsCaption.EndsWith("No Limit Hold'em"))
				{
					Rect rect = new Rect();
					GetWindowRect(hWnd, ref rect);
					Invoke((MethodInvoker)delegate()
					{
						Location = new Point(rect.Left, rect.Top);
						Size = new Size(rect.Right - rect.Left, rect.Bottom - rect.Top);
						label1.Text = windowsCaption;
						label2.Text = string.Format("({0},{1})[{2}x{3}]", Location.X, Location.Y, Size.Width, Size.Height);
						OnResizeEnd(EventArgs.Empty);
					});
				}
			}
			return true;
		}

		protected override void OnResizeEnd(EventArgs e)
		{
			base.OnResizeEnd(e);
			double ratio = TableLayout.GetRatio(Width, Height);
			for (int c = 0; c < TableLayout.Seats.Length; c++)
			{
				Label label = Labels[c];
				Point loc = TableLayout.GetSeat(c, ratio);
				label.Location = new Point(loc.X, loc.Y);
			}
		}

		public void RefreshSizeAndPosition()
		{
			EnumWindows(EnumTheWindows, (IntPtr)0);
		}

		private TableLayoutInformations TableLayout = new TableLayoutInformations();
		private List<Label> Labels = new List<Label>();

		private void Start()
		{
			try
			{
				while (true)
				{
					RefreshSizeAndPosition();
					Thread.Sleep(1000);
				}
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message);
			}
		}

		public PokerTableOverlay()
		{
			InitializeComponent();
			Thread thread = new Thread(Start);
			thread.Start();

			Size = new Size(TableLayout.Size.Width, TableLayout.Size.Height);

			for (int c = 0; c < TableLayout.Seats.Length; c++)
			{
				Label label = new Label();
				Controls.Add(label);
				Labels.Add(label);
				label.BackColor = Color.LightGray;
				label.Text = "Seat #" + (c + 1);
				Point loc = TableLayout.GetSeat(c, 1);
				label.Location = new Point(loc.X, loc.Y);
			}
		}
	}
}
